package com.zzzyh.forumsystemclient.services.impl;

import com.zzzyh.forumsystemclient.dao.MessageMapper;
import com.zzzyh.forumsystemclient.dao.UserMapper;
import com.zzzyh.forumsystemclient.dao.User_UserMapper;
import com.zzzyh.forumsystemclient.model.Friend;
import com.zzzyh.forumsystemclient.model.User;
import com.zzzyh.forumsystemclient.model.User_User;
import com.zzzyh.forumsystemclient.services.IUser_UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Slf4j
@Service

public class User_UserServiceImpl implements IUser_UserService {
    @Autowired
    private User_UserMapper user_userMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public List<Friend> getContacts(Long userId) {
        List<Long> contactsId= user_userMapper.getContacts(userId);
        List<Friend> friends = new ArrayList<>();
        for (Long contactId : contactsId){
            Friend friend = new Friend();
            User user = userMapper.selectByPrimaryKey(contactId);
            friend.setId(user.getId());
            friend.setUsername(user.getNickname()+"@"+user.getUsername());
            friends.add(friend);
        }
        return friends;
    }

    @Override
    public List<Friend> getFollowers(Long userId) {
        List<Long> followersId = user_userMapper.getFollowers(userId);
        List<Friend> friends = new ArrayList<>();
        log.info("followersId:{}",followersId);
        for (Long followerId : followersId){
            if (followerId == null)
                continue;
            Friend friend = new Friend();
            User user = userMapper.selectByPrimaryKey(followerId);
            friend.setId(user.getId());
            friend.setUsername(user.getNickname()+"@"+user.getUsername());
            friends.add(friend);
        }
        return friends;
    }

    @Override
    public List<Friend> getStrangers(Long userId) {
        List<Long> sendUsers = messageMapper.getSendUsers(userId);
        List<Long> followersId = user_userMapper.getFollowers(userId);
        List<Long> contactsId= user_userMapper.getContacts(userId);

        List<Long> uniqueSendUsers = new ArrayList<>(new HashSet<>(sendUsers));
        // 假设 sendUsers 是 List<Long> 类型的用户列表
        List<Long> combinedIds = new ArrayList<>();
        combinedIds.addAll(followersId); // 添加粉丝 ID
        combinedIds.addAll(contactsId);   // 添加联系人 ID

        // 创建一个新的列表，只包含不在 combinedIds 中的用户
        List<Long> filteredSendUsers = new ArrayList<>();
        log.info("combinedIds:{}",combinedIds);
        log.info("sendUserid{}",sendUsers);
        for (Long user : uniqueSendUsers) {
            if (!combinedIds.contains(user)) {
                filteredSendUsers.add(user);
            }
        }
        List<Friend> friends = new ArrayList<>();
        for (Long strangerId : filteredSendUsers){
            Friend friend = new Friend();
            User user = userMapper.selectByPrimaryKey(strangerId);
            friend.setId(user.getId());
            friend.setUsername(user.getNickname()+"@"+user.getUsername());
            friends.add(friend);
        }
        return friends;
    }

    @Override
    public Boolean getUserFollowAuthor(Long userId, Long authorId) {
        Integer relationship = user_userMapper.getRelationship(userId, authorId);
        if(relationship==null){
            relationship = user_userMapper.getRelationship(authorId, userId);
            if (relationship  == null)
                return false;
            if (relationship==1)
                return true;
            if (relationship==3)
                return true;
            return false;
        }
        if (relationship==1 || relationship==2)
            return true;
        relationship = user_userMapper.getRelationship(authorId, userId);
        if(relationship==null)
            return false;
        if (relationship==1 || relationship==3)
            return true;
        return false;
    }

    @Override
    // 0无关1互关2前关注后3后关注前
    public void unFollowAuthor(Long userId, Long authorId) {
        Integer relationship = user_userMapper.getRelationship(userId, authorId);
        if(relationship==null){
            relationship = user_userMapper.getRelationship(authorId, userId);
            if(relationship==null){

            }
            else if(relationship == 1)
                user_userMapper.updateRelationship(authorId, userId, 2);
            else if(relationship == 2) {

            } else if (relationship == 3) {
                user_userMapper.updateRelationship(authorId, userId, 0);
            }
        } else if (relationship == 0) {

        } else if (relationship==1){
            user_userMapper.updateRelationship(userId, authorId, 3);
        }
        else if (relationship==2){
            user_userMapper.updateRelationship(userId, authorId, 0);
        } else if (relationship == 3) {

        }
    }

    @Override
    public void followAuthor(Long userId, Long authorId) {
        Integer relationship = user_userMapper.getRelationship(userId, authorId);
        if(relationship==null){
            relationship = user_userMapper.getRelationship(authorId, userId);
            if(relationship==null) {
                User_User userUser = new User_User();
                userUser.setUser1Id(userId);
                userUser.setUser2Id(authorId);
                userUser.setState((byte) 2);
                userUser.setIsDelete((byte) 0);
                user_userMapper.insert(userUser);
            }
            else {
                if (relationship == 0){
                    user_userMapper.updateRelationship(authorId, userId, 3);
                }else if(relationship == 1){

                }else if(relationship == 2){
                    user_userMapper.updateRelationship(authorId, userId, 1);
                } else if (relationship == 3) {
                }
            }
        }else if (relationship == 0){
            user_userMapper.updateRelationship(userId, authorId, 2);
        } else if (relationship == 1) {

        }else if (relationship == 2) {

        } else if (relationship ==3) {
            user_userMapper.updateRelationship(userId, authorId, 1);
        }
    }


}
